package com.xzh.springCloud.controller;

import com.xzh.springCloud.entity.Member;
import com.xzh.springCloud.entity.Result;
import com.xzh.springCloud.service.MemberOpenFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author: zhiHong
 * @CreateTime: 2023-08-29 10:42
 * @Description: 控制层
 */
@RestController
@Slf4j
public class MemberNacosConsumerController {
    // 这里的 IP 地址对应的就是注册到 Nacos 的服务名
    public static final String MEMBER_SERVICE_PROVIDER_URL = "http://member-service-nacos-provider";

    @Resource
    private RestTemplate restTemplate;

    // 装配 MemberOpenFeignService
    @Resource
    private MemberOpenFeignService memberOpenFeignService;

    @GetMapping("/member/openfeign/consumer/get/{id}")
    public Result getMemberOpenFeignById(@PathVariable("id") Long id) {
        return memberOpenFeignService.queryMemberById(id);
    }
    @PostMapping("/member/nacos/consumer/save")
    public Result saveMember(@RequestBody Member member) {
        log.info("info = {}", member.toString());
        return restTemplate.postForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/save/", member, Result.class);
    }

    @GetMapping("/member/nacos/consumer/get/{id}")
    public Result getMemberById(@PathVariable("id") Long id) {
        return restTemplate.getForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/get/" + id, Result.class);
    }
}
